package com.ctsi.security.app.controller;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.framework.common.response.UnifiedResponse;
import com.ctsi.framework.security.util.SecurityUtil;
import com.ctsi.security.I_PermisionTypeExtend;
import com.ctsi.security.PartySrole;
import com.ctsi.security.PermissionSer;
import com.ctsi.security.PermissionTypeSer;
import com.ctsi.security.app.permissiontree.PermisionTypeNode;
import com.ctsi.security.domain.Permission;
import com.ctsi.security.domain.PermissionType;
import com.ctsi.security.server.PermisionTypeExtendManage;
import com.ctsi.security.service.RoleSecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SecurityTreeController {
	private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(SecurityTreeController.class);
	@Autowired
	private PermissionTypeSer permissionTypeService;
    @Autowired
	private PermissionSer permissionService;
    @Autowired
	private RoleSecurityService roleSecurityService;
    @Autowired
	private PartySrole secSRoleService;


//	public ModelAndView selectTree(HttpServletRequest request, HttpServletResponse response) {
//
//		return new ModelAndView("security/choice/radioTree");
//	}
//
//	public ModelAndView roleDataPage(HttpServletRequest request, HttpServletResponse response) {
//
//		int skipResults = 0;
//		int pageSize = Page.DEFAULT_PAGE_SIZE;
//		String start = request.getParameter("start");
//		String limit = request.getParameter("limit");
//		if (limit != null && !limit.trim().equals("")) {
//			pageSize = Integer.parseInt(limit);
//		}
//		if (start != null && !start.trim().equals(""))
//			skipResults = Integer.parseInt(start);
//		/**
//		 * gyz modified at 2008-3-28
//		 */
//		// 当前登录用户
//		String userLogin = getHandleUserLogin().getLoginUserId(request);
//		// 当前方法的功能ID
//		String permissionId = getPermissionId("roleDataPage");
//		logger.debug("方法{}的功能ID为{}", "roleDataPage", permissionId);
//		// Page page = roleService.queryRole(pageSize, skipResults);
//		// 增加查询功能
//		String roleName = request.getParameter("roleName");
//
//		Map<String, Object> params = new HashMap<String, Object>();
//		if (UtilValidate.isNotEmpty(roleName)) {
//			roleName = roleName.trim();
//			params.put("roleName", "%" + roleName + "%");
//		}
//		String moduleType = request.getParameter("moduleType");
//		if (UtilValidate.isNotEmpty(moduleType)) {
//			params.put("moduleType", moduleType);
//		}
//
//		Page page = secSRoleService.queryRole(userLogin, "sysModule", "SELECT", pageSize, skipResults, params);
//		/**
//		 * /** gyz modified at 2008-3-28
//		 */
//		PageUtil out = new PageUtil(page);
//		super.outJson(out, response);
//		return null;
//	}
//

	/**
	 *
	 * @param parentNode
	 * @return
	 */
	@PostMapping(value = "/security/child-permission",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public UnifiedResponse<List<Permission>> findChildPermision(@RequestBody PermisionTypeNode parentNode) {
		String parent = null;
		if (UtilValidate.isNotEmpty(parentNode.getInExtends())) {
			parent = parentNode.getLastId();
		} else {
			parent = parentNode.getId();
		}
		List<Permission> plist = PermisionTypeExtendManage.getInstance().getPermision(parent, SecurityUtil.getUser().getLoginId(), parentNode.getIdBeans(), parentNode.getPaths());
		return UnifiedResponse.ok(plist);
	}

	/**
	 * 权限树
	 * @param parent
	 * @return
	 * @throws CloneNotSupportedException
	 */
	@PostMapping(value = "/security/permission-tree",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public UnifiedResponse<List<Map<String,Object>>> permisionTypeTreeWithExtend(@RequestBody PermisionTypeNode parent) throws CloneNotSupportedException {
		String parentId = null;
		PermisionTypeNode parentNode = new PermisionTypeNode();
		if (UtilValidate.isEmpty(parent.getId()) || "ROOT".equalsIgnoreCase(parent.getId())) {
			parentId = "0";
			parentNode.addPath(parentId, "");
		} else {
			parentNode = parent;
			parentId = parentNode.getId();
		}
		I_PermisionTypeExtend ext = null;
		List<PermissionType> localListPermType = new ArrayList<>();

		if (UtilValidate.isNotEmpty(parentNode.getInExtends())) {
			ext = PermisionTypeExtendManage.getInstance().getPermisionTypeExtendById(parentNode.getInExtends());
		}
		if (ext == null) {
			ext = PermisionTypeExtendManage.getInstance().getPermisionTypeExtendByParent(parentId);
		}

		// 判断是否具有扩展点
		if (ext != null) {
			localListPermType = ext.getPermissionTypes(parentId,SecurityUtil.getUser().getUserId(), SecurityUtil.getUser().getLoginId());
			if (parentId.equals(ext.getRoot())) {
				parentNode.addBean(ext.getId());
				parentNode.setLastId(parentId);
				parentNode.setInExtends(ext.getId());
			}
			if (UtilValidate.isEmpty(localListPermType)) {
				parentNode.setInExtends("");
				parentId = parentNode.getLastId();
			}

		}
		if (UtilValidate.isEmpty(localListPermType)) {
			localListPermType = permissionTypeService.findAllSon(SecurityUtil.getUser().getLoginId(), "sysModule", "SELECT", parentId);
		}
		List<Map<String,Object>> json = new ArrayList<>();
		for (PermissionType permissionType : localListPermType) {
			PermisionTypeNode thisNode = (PermisionTypeNode) parentNode.clone();
			thisNode.addPath(permissionType.getPermissionTypeId(), permissionType.getPermissionTypeName());
			thisNode.setId(permissionType.getPermissionTypeId());
			thisNode.setName(permissionType.getPermissionTypeName());
			Map<String, Object> map = new HashMap<>();
			map.put("id", permissionType.getPermissionTypeId());
			map.put("value", thisNode);
			map.put("path", thisNode.getPathStr());
			map.put("pathname", thisNode.getPathNameStr());
			map.put("name", permissionType.getPermissionTypeName());
			map.put("checked", false);
			map.put("isParent", true);
			// map.put("leaf", isleaf);
			json.add(map);
		}
		return UnifiedResponse.ok(json);
	}

	/************************* 功能类型树***************start ***********************************/


	@GetMapping(value = "/security/permission-type-tree")
	public UnifiedResponse<List<Map<String,Object>> > permTypeTree(HttpServletRequest request, HttpServletResponse response) {
		String parentId = request.getParameter("parentId");
		parentId = parentId == null || parentId.equals("") ? "root" : parentId.trim();
		StringBuilder sb = new StringBuilder("[");
		List<Map<String,Object>> lists = new ArrayList<>();
		Map<String,Object> maps = new HashMap<>();

		String userLoginId = SecurityUtil.getUser().getLoginId();

		if (parentId.equals("root")) {
			List<PermissionType> listPermType = permissionTypeService.findAllSon("0");
			boolean leafValue = true;
			if (listPermType.size() > 0) {
				leafValue = false;
			}
			maps.put("id", "0");
			maps.put("value", "0");
			maps.put("expanded",true);
			maps.put("text", "根结点");
			maps.put("leaf", leafValue);
			maps.put("checked",false);
			List<Map<String,Object>> list = new ArrayList<>();
			for (PermissionType permType : listPermType) {
				Map<String,Object> map = new HashMap<>();
				boolean localLeafValue = true;
				List<PermissionType> localListPermType = permissionTypeService.findAllSon(permType.getPermissionTypeId());
				if (localListPermType.size() > 0) {
					localLeafValue = false;
				}
				map.put("id", permType.getPermissionTypeId());
				map.put("value",permType.getPermissionTypeId());
				map.put("text", permType.getPermissionTypeName());
				map.put("leaf", localLeafValue);
				map.put("checked",false);
				list.add(map);
			}
			maps.put("children", list);
			lists.add(maps);
		} else {
			List<PermissionType> listPermType = permissionTypeService.findAllSon(parentId);
			for (PermissionType permType : listPermType) {
				Map<String,Object> map = new HashMap<>();
				boolean localLeafValue = true;
				List<PermissionType> localListPermType = permissionTypeService.findAllSon(permType.getPermissionTypeId());
				if (localListPermType.size() > 0) {
					localLeafValue = false;
				}
				map.put("id", permType.getPermissionTypeId());
				map.put("value", permType.getPermissionTypeId());
				map.put("text",permType.getPermissionTypeName());
				map.put("leaf", localLeafValue);
				map.put("checked",false);
				lists.add(map);
			}
		}
		return UnifiedResponse.ok(lists);
	}

//	public ModelAndView showPermissionTreeWindow(HttpServletRequest request, HttpServletResponse response) {
//		return new ModelAndView("jportal/permissiontree");
//	}
//
//	/************************* 功能类型树***************end *************************************/
//	/************************* 功能树***************start *************************************/
//	public ModelAndView permissionTree(HttpServletRequest request, HttpServletResponse response) {
//		String parentId = request.getParameter("id");
//		parentId = parentId == null || parentId.equals("") ? "root" : parentId.trim();
//		StringBuilder sb = new StringBuilder("[");
//		if (parentId.equals("root")) {
//			List<PermissionType> listPermType = permissionTypeService.findAllSon("0");
//			String leafValue = "true";
//			if (listPermType.size() > 0) {
//				leafValue = "false";
//			}
//			sb.append("{\"id\":\"0\",\"value\":\"0\",\"name\":\"根节点\",\"expanded\":true,\"isParent\":" + true + ",\"children\": [");
//			for (PermissionType permType : listPermType) {
//				String localLeafValue = "true";
//				List<PermissionType> localListPermType = permissionTypeService.findAllSon(permType.getPermissionTypeId());
//				if (localListPermType.size() > 0) {
//					localLeafValue = "false";
//					sb.append("{\"id\":\"" + permType.getPermissionTypeId() + "\",\"value\":\"" + permType.getPermissionTypeId() + "\",\"name\":\"" + permType.getPermissionTypeName() + "\",\"isParent\":" + true + "},");
//				} else {
//					// 从功能表中取该功能类型的所有功能
//					List<Permission> listPermission = permissionService.findPermission(new String[] { "permissionTypeId" }, new Object[] { permType.getPermissionTypeId() });
//					// 有功能
//					if (listPermission.size() > 0) {
//						localLeafValue = "false";
//						sb.append("{\"id\":\"" + permType.getPermissionTypeId() + "\",\"value\":\"" + permType.getPermissionTypeId() + "\",\"name\":\"" + permType.getPermissionTypeName() + "\",\"isParent\":" + true + ",\"children\": [");
//						for (Permission permission : listPermission) {
//							sb.append("{\"id\":\"" + permission.getPermissionId() + "\",\"value\":\"" + permission.getPermissionId() + "\",\"name\":\"" + permission.getPermissionName() + "\",\"isParent\":true,\"checked\":false},");
//						}
//						sb.deleteCharAt(sb.length() - 1);
//						sb.append("]},");
//					} else {
//						// 没有功能
//						sb.append("{\"id\":\"" + permType.getPermissionTypeId() + "\",\"value\":\"" + permType.getPermissionTypeId() + "\",\"name\":\"" + permType.getPermissionTypeName() + "\",\"isParent\":" + true + "},");
//					}
//				}
//			}
//			sb.deleteCharAt(sb.length() - 1);
//			sb.append("]}");
//		} else {
//			List<PermissionType> listPermType = permissionTypeService.findAllSon(parentId);
//			for (PermissionType permType : listPermType) {
//				String localLeafValue = "true";
//				List<PermissionType> localListPermType = permissionTypeService.findAllSon(permType.getPermissionTypeId());
//				if (localListPermType.size() > 0) {
//					localLeafValue = "false";
//					sb.append("{\"id\":\"" + permType.getPermissionTypeId() + "\",\"value\":\"" + permType.getPermissionTypeId() + "\",\"name\":\"" + permType.getPermissionTypeName() + "\",\"isParent\":" + true + "},");
//				} else {
//					// 从功能表中取该功能类型的所有功能
//					List<Permission> listPermission = permissionService.findPermission(new String[] { "permissionTypeId" }, new Object[] { permType.getPermissionTypeId() });
//					// 有功能
//					if (listPermission.size() > 0) {
//						localLeafValue = "false";
//						sb.append("{\"id\":\"" + permType.getPermissionTypeId() + "\",\"value\":\"" + permType.getPermissionTypeId() + "\",\"name\":\"" + permType.getPermissionTypeName() + "\",\"isParent\":" + true + ",\"children\": [");
//						for (Permission permission : listPermission) {
//							sb.append("{\"id\":\"" + permission.getPermissionId() + "\",\"value\":\"" + permission.getPermissionId() + "\",\"name\":\"" + permission.getPermissionName() + "\",\"isParent\":true,\"checked\":false},");
//						}
//						sb.deleteCharAt(sb.length() - 1);
//						sb.append("]},");
//					} else {
//						// 没有功能
//						sb.append("{\"id\":\"" + permType.getPermissionTypeId() + "\",\"value\":\"" + permType.getPermissionTypeId() + "\",\"name\":\"" + permType.getPermissionTypeName() + "\",\"isParent\":" + true + "},");
//					}
//				}
//			}
//			sb.deleteCharAt(sb.length() - 1);
//			if(UtilValidate.isEmpty(listPermType)){
//				sb.append("[");
//			}
//		}
//		sb.append("]");
//		outJson(sb.toString(), response);
//		return null;
//	}
//
//	/**
//	 * 权限功能复选树（gyz added at 2008-4-2）
//	 *
//	 * @param request
//	 * @param response
//	 * @return
//	 */
//	public ModelAndView checkedTree(HttpServletRequest request, HttpServletResponse response) {
//		String parentId = request.getParameter("parentId");
//		// 获得当前登录用户
//		String userLogin = getHandleUserLogin().getLoginUserId(request);
//		// 当前树的权限Id
//		// String permission=getPermissionId("checkedTree");
//		parentId = parentId == null || parentId.equals("") ? "root" : parentId.trim();
//		StringBuilder sb = new StringBuilder("[");
//		if (parentId.equals("root")) {
//			List<PermissionType> listPermType = permissionTypeService.findAllSon(userLogin, "sysModule", "SELECT", "0");
//			String leafValue = "true";
//			if (listPermType.size() > 0) {
//				leafValue = "false";
//			}
//			sb.append("{id:'0',value:'0',text:'根结点',expanded:true,leaf:" + leafValue + ",children: [");
//			for (PermissionType permType : listPermType) {
//				String localLeafValue = "true";
//				List<PermissionType> localListPermType = permissionTypeService.findAllSon(userLogin, "sysModule", "SELECT", permType.getPermissionTypeId());
//				if (localListPermType.size() > 0) {
//					localLeafValue = "false";
//					sb.append("{id:'" + permType.getPermissionTypeId() + "',value:'" + permType.getPermissionTypeId() + "',text:'" + permType.getPermissionTypeName() + "',leaf:" + localLeafValue + "},");
//				} else {
//					// 从功能表中取该功能类型的所有功能
//					// List<Permission> listPermission=permissionService.findPermission(new String[]{"permissionTypeId"}, new
//					// Object[]{permType.getPermissionTypeId()});
//					List<Permission> listPermission = permissionService.findPermission(userLogin, "sysModule", "SELECT", UtilMisc.toMap("permissionTypeId", permType.getPermissionTypeId()));
//					// 有功能
//					if (listPermission.size() > 0) {
//						localLeafValue = "false";
//						sb.append("{id:'" + permType.getPermissionTypeId() + "',value:'" + permType.getPermissionTypeId() + "',text:'" + permType.getPermissionTypeName() + "',leaf:" + localLeafValue + ",children: [");
//						for (Permission permission1 : listPermission) {
//							sb.append("{id:'" + permission1.getPermissionId() + "',value:'" + permission1.getPermissionId() + "',text:'" + permission1.getPermissionName() + "',leaf:true,checked:false},");
//						}
//						sb.deleteCharAt(sb.length() - 1);
//						sb.append("]},");
//					} else {
//						// 没有功能
//						sb.append("{id:'" + permType.getPermissionTypeId() + "',value:'" + permType.getPermissionTypeId() + "',text:'" + permType.getPermissionTypeName() + "',leaf:" + localLeafValue + ",checked:false},");
//					}
//				}
//			}
//			sb.deleteCharAt(sb.length() - 1);
//			sb.append("]}");
//		} else {
//			List<PermissionType> listPermType = permissionTypeService.findAllSon(userLogin, "sysModule", "SELECT", parentId);
//			// 取得该功能类型下的所有的功能ID
//			List<Permission> parentPermission = permissionService.findPermission(userLogin, "sysModule", "SELECT", UtilMisc.toMap("permissionTypeId", parentId));
//			if (UtilValidate.isNotEmpty(parentPermission)) {
//				for (Permission permission1 : parentPermission) {
//					sb.append("{id:'" + permission1.getPermissionId() + "',value:'" + permission1.getPermissionId() + "',text:'" + permission1.getPermissionName() + "',leaf:true,checked:false},");
//				}
//			}
//			for (PermissionType permType : listPermType) {
//				String localLeafValue = "true";
//				List<PermissionType> localListPermType = permissionTypeService.findAllSon(userLogin, "sysModule", "SELECT", permType.getPermissionTypeId());
//				if (localListPermType.size() > 0) {
//					localLeafValue = "false";
//					sb.append("{id:'" + permType.getPermissionTypeId() + "',value:'" + permType.getPermissionTypeId() + "',text:'" + permType.getPermissionTypeName() + "',leaf:" + localLeafValue + "},");
//				} else {
//					// 从功能表中取该功能类型的所有功能
//					// 加入对数据权限的控制
//					List<String> dataObjectIds = super.getSecurity().getDataObjectIds(userLogin, "sysModule", "SELECT");
//					logger.info("DataObjects is {}", dataObjectIds);
//					// List<Permission> listPermission=permissionService.findPermission(new String[]{"permissionTypeId"}, new
//					// Object[]{permType.getPermissionTypeId()});
//					List<Permission> listPermission = permissionService.findPermission(userLogin, "sysModule", "SELECT", UtilMisc.toMap("permissionTypeId", permType.getPermissionTypeId()));
//
//					// 有功能
//					if (listPermission.size() > 0) {
//						localLeafValue = "false";
//						sb.append("{id:'" + permType.getPermissionTypeId() + "',value:'" + permType.getPermissionTypeId() + "',text:'" + permType.getPermissionTypeName() + "',leaf:" + localLeafValue + ",children: [");
//						for (Permission permission1 : listPermission) {
//							sb.append("{id:'" + permission1.getPermissionId() + "',value:'" + permission1.getPermissionId() + "',text:'" + permission1.getPermissionName() + "',leaf:true,checked:false},");
//						}
//						sb.deleteCharAt(sb.length() - 1);
//						sb.append("]},");
//					} else {
//						// 没有功能,则不显示
//						logger.info("{}具有对功能类型{}的数据权限，但没有包含功能IDS", userLogin, permType.getPermissionTypeId());
//						// sb.append("{id:'"+permType.getPermissionTypeId()+"',value:'"+permType.getPermissionTypeId()+"',text:'"+permType.getPermissionTypeName()+"',leaf:"+localLeafValue+",checked:false},");
//					}
//				}
//			}
//			if (sb.indexOf(",") != -1) {
//				sb.deleteCharAt(sb.length() - 1);
//			}
//		}
//		sb.append("]");
//		if (logger.isDebugEnabled()) {
//			logger.debug("权限功能树所对应的JSON数据为{}", sb);
//		}
//		outJson(sb.toString(), response);
//		return null;
//	}
//
//	/**
//	 * 权限功能复选树（过滤到用户已有功能）
//	 *
//	 * @param request
//	 * @param response
//	 * @return
//	 */
//	public ModelAndView checkedTreeFilter(HttpServletRequest request, HttpServletResponse response) {
//		String parentId = request.getParameter("parentId");
//		// 获得当前登录用户
//		String userLogin = getHandleUserLogin().getLoginUserId(request);
//		String roleId = request.getParameter("roleId");
//		// 当前树的权限Id
//		// String permission=getPermissionId("checkedTree");
//		parentId = parentId == null || parentId.equals("") ? "root" : parentId.trim();
//		StringBuilder sb = new StringBuilder("[");
//		if (parentId.equals("root")) {
//			List<PermissionType> listPermType = permissionTypeService.findAllSon(userLogin, "sysModule", "SELECT", "0");
//			bizLog.logInfo(sysInfo.getSysName(), "rightManagement", BizlogType.SECURE, "check", "查看成功", "查看根目录下所有功能类型", BizLog.VERBOSE, "查看权限功能类型", "parentId:"+parentId);
//			String leafValue = "true";
//			if (listPermType.size() > 0) {
//				leafValue = "false";
//			}
//			sb.append("{id:'0',value:'0',text:'根结点',expanded:true,leaf:" + leafValue + ",children: [");
//			for (PermissionType permType : listPermType) {
//				String localLeafValue = "true";
//				List<PermissionType> localListPermType = permissionTypeService.findAllSon(userLogin, "sysModule", "SELECT", permType.getPermissionTypeId());
//				bizLog.logInfo(sysInfo.getSysName(), "rightManagement", BizlogType.SECURE, "check", "查看成功", "查看"+permType.getPermissionTypeName()+"下所有功能类型", BizLog.VERBOSE, "查看权限功能类型", "permissionTypeId:"+permType.getPermissionTypeId());
//				if (localListPermType.size() > 0) {
//					localLeafValue = "false";
//					sb.append("{id:'" + permType.getPermissionTypeId() + "',value:'" + permType.getPermissionTypeId() + "',text:'" + permType.getPermissionTypeName() + "',leaf:" + localLeafValue + "},");
//				} else {
//					// 从功能表中取该功能类型的所有功能
//					// List<Permission> listPermission=permissionService.findPermission(new String[]{"permissionTypeId"}, new
//					// Object[]{permType.getPermissionTypeId()});
//					List<Permission> listPermission = permissionService.findPermission(userLogin, "sysModule", "SELECT", UtilMisc.toMap("permissionTypeId", permType.getPermissionTypeId()));
//					bizLog.logInfo(sysInfo.getSysName(), "rightManagement", BizlogType.SECURE, "check", "查看成功", "查看"+permType.getPermissionTypeName()+"下所有功能", BizLog.VERBOSE, "查看权限功能类型下的所有功能", "permissionTypeId:"+permType.getPermissionTypeId());
//					List<RolePermission> rolePerms = roleSecurityService.findRolePermission(new String[] { "roleid" }, new String[] { roleId });
//					bizLog.logInfo(sysInfo.getSysName(), "rightManagement", BizlogType.SECURE, "check", "查看成功", "查看权限角色所具有的功能", BizLog.VERBOSE, "查看权限角色所具有的功能", "roleId:"+roleId);
//					removeAlready(listPermission, rolePerms);
//					// 有功能
//					if (listPermission.size() > 0) {
//						localLeafValue = "false";
//						sb.append("{id:'" + permType.getPermissionTypeId() + "',value:'" + permType.getPermissionTypeId() + "',text:'" + permType.getPermissionTypeName() + "',leaf:" + localLeafValue + ",children: [");
//						for (Permission permission1 : listPermission) {
//							sb.append("{id:'" + permission1.getPermissionId() + "',value:'" + permission1.getPermissionId() + "',text:'" + permission1.getPermissionName() + "',leaf:true,checked:false},");
//						}
//						sb.deleteCharAt(sb.length() - 1);
//						sb.append("]},");
//					} else {
//						// 没有功能
//						// sb.append("{id:'"+permType.getPermissionTypeId()+"',value:'"+permType.getPermissionTypeId()+"',text:'"+permType.getPermissionTypeName()+"',leaf:false},");
//					}
//				}
//			}
//			sb.deleteCharAt(sb.length() - 1);
//			sb.append("]}");
//		} else {
//			List<PermissionType> listPermType = permissionTypeService.findAllSon(userLogin, "sysModule", "SELECT", parentId);
//			PermissionType permissionType = permissionTypeService.findByPermisionTypeId(parentId);
//			String permTypeName = "";
//			if(permissionType!=null){
//				permTypeName = permissionType.getPermissionTypeName();
//			}
//			bizLog.logInfo(sysInfo.getSysName(), "rightManagement", BizlogType.SECURE, "check", "查看成功", "查看"+permTypeName+"下所有功能类型", BizLog.VERBOSE, "查看权限功能类型", "parentId:"+parentId);
//			// 取得该功能类型下的所有的功能ID
//			List<Permission> parentPermission = permissionService.findPermission(userLogin, "sysModule", "SELECT", UtilMisc.toMap("permissionTypeId", parentId));
//			bizLog.logInfo(sysInfo.getSysName(), "rightManagement", BizlogType.SECURE, "check", "查看成功", "查看"+permTypeName+"下所有功能", BizLog.VERBOSE, "查看权限功能类型下的所有功能", "parentId:"+parentId);
//			List<RolePermission> rolePerms = roleSecurityService.findRolePermission(new String[] { "roleid" }, new String[] { roleId });
//			removeAlready(parentPermission, rolePerms);
//			if (UtilValidate.isNotEmpty(parentPermission)) {
//				for (Permission permission1 : parentPermission) {
//					sb.append("{id:'" + permission1.getPermissionId() + "',value:'" + permission1.getPermissionId() + "',text:'" + permission1.getPermissionName() + "',leaf:true,checked:false},");
//				}
//			}
//			for (PermissionType permType : listPermType) {
//				String localLeafValue = "true";
//				List<PermissionType> localListPermType = permissionTypeService.findAllSon(userLogin, "sysModule", "SELECT", permType.getPermissionTypeId());
//				if (localListPermType.size() > 0) {
//					localLeafValue = "false";
//					sb.append("{id:'" + permType.getPermissionTypeId() + "',value:'" + permType.getPermissionTypeId() + "',text:'" + permType.getPermissionTypeName() + "',leaf:" + localLeafValue + "},");
//				} else {
//					// 从功能表中取该功能类型的所有功能
//					// 加入对数据权限的控制
//					List<String> dataObjectIds = super.getSecurity().getDataObjectIds(userLogin, "sysModule", "SELECT");
//					logger.info("DataObjects is {}", dataObjectIds);
//					// List<Permission> listPermission=permissionService.findPermission(new String[]{"permissionTypeId"}, new
//					// Object[]{permType.getPermissionTypeId()});
//					List<Permission> listPermission = permissionService.findPermission(userLogin, "sysModule", "SELECT", UtilMisc.toMap("permissionTypeId", permType.getPermissionTypeId()));
//					rolePerms = roleSecurityService.findRolePermission(new String[] { "roleid" }, new String[] { roleId });
//					bizLog.logInfo(sysInfo.getSysName(), "rightManagement", BizlogType.SECURE, "check", "查看成功", "查看权限角色所具有的功能", BizLog.VERBOSE, "查看权限角色所具有的功能", "roleId:"+roleId);
//					removeAlready(listPermission, rolePerms);
//					// 有功能
//					if (listPermission.size() > 0) {
//						localLeafValue = "false";
//						sb.append("{id:'" + permType.getPermissionTypeId() + "',value:'" + permType.getPermissionTypeId() + "',text:'" + permType.getPermissionTypeName() + "',leaf:" + localLeafValue + ",children: [");
//						for (Permission permission1 : listPermission) {
//							sb.append("{id:'" + permission1.getPermissionId() + "',value:'" + permission1.getPermissionId() + "',text:'" + permission1.getPermissionName() + "',leaf:true,checked:false},");
//						}
//						sb.deleteCharAt(sb.length() - 1);
//						sb.append("]},");
//					} else {
//						// 没有功能,则不显示
//						logger.info("{}具有对功能类型{}的数据权限，但没有包含功能IDS", userLogin, permType.getPermissionTypeId());
//						// sb.append("{id:'"+permType.getPermissionTypeId()+"',value:'"+permType.getPermissionTypeId()+"',text:'"+permType.getPermissionTypeName()+"',leaf:"+localLeafValue+",checked:false},");
//					}
//				}
//			}
//			if (sb.indexOf(",") != -1) {
//				sb.deleteCharAt(sb.length() - 1);
//			}
//		}
//		sb.append("]");
//		if (logger.isDebugEnabled()) {
//			logger.debug("权限功能树所对应的JSON数据为{}", sb);
//		}
//		outJson(sb.toString(), response);
//		return null;
//	}
//
//	/************************* 功能树***************end *************************************/
//
//	private void removeAlready(List<Permission> listPermission, List<RolePermission> rolePerms) {
//		if (UtilValidate.isEmpty(rolePerms) || UtilValidate.isEmpty(listPermission)) {
//			return;
//		}
//		Iterator<Permission> iterator = listPermission.iterator();
//		while (iterator.hasNext()) {
//			if (containsPerm(rolePerms, iterator.next())) {
//				iterator.remove();
//			}
//		}
//	}
//
//	private boolean containsPerm(List<RolePermission> rolePerms, Permission perm) {
//		for (RolePermission rp : rolePerms) {
//			if (rp.getPermissionId().equals(perm.getPermissionId())) {
//				return true;
//			}
//		}
//		return false;
//	}
//
//	/**
//	 * 数据对象类型树（gyz added at 2008-3-31）
//	 *
//	 * @param request
//	 * @param response
//	 * @return
//	 */
//	public ModelAndView dataObjectTypeTree(HttpServletRequest request, HttpServletResponse response) {
//		StringBuilder builder = new StringBuilder();
//		String parent = request.getParameter("parentId");
//		logger.info(parent);
//		List<DataObjectType> list = new LinkedList<DataObjectType>();
//		// 判断是否为空
//		if (UtilValidate.isEmpty(parent)) {
//			list.addAll(roleSecurityService.queryDataObjectType(null));
//			logger.info("list size is {}", list.size());
//		} else {
//			list.addAll(roleSecurityService.queryDataObjectType(parent));
//		}
//		// 输出JSON数据
//		builder.append("[");
//		for (DataObjectType type : list) {
//			builder.append("{");
//			builder.append("id:'").append(UUID.randomUUID().toString()).append("'");
//			builder.append(",value:'").append(type.getDataObjectTypeId()).append("'");
//			builder.append(",text:'").append(type.getDataObjectTypeName()).append("'");
//			builder.append(",checked:false");
//			builder.append("},");
//		}
//		// 去掉最后一个,
//		if (builder.indexOf(",") != -1) {
//			builder.deleteCharAt(builder.lastIndexOf(","));
//		}
//		builder.append("]");
//		logger.info("dataTypeTree:{}", builder);
//		outJson(builder.toString(), response);
//		return null;
//	}
//
//	public String list2Json(List<?> list) {
//		if (UtilValidate.isEmpty(list)) {
//			return "[]";
//		}
//		StringBuilder json = new StringBuilder();
//
//		for (Object value : list) {
//			if (json.length() != 0) {
//				json.append(',');
//			}
//			if (value instanceof List) {
//				json.append(list2Json((List) value));
//			} else if (value instanceof Map) {
//				json.append(map2Json((Map<String, Object>) value));
//			} else if (value instanceof String) {
//				json.append('\'').append(value).append('\'');
//			} else {
//				json.append(value);
//			}
//		}
//
//		json = new StringBuilder("[").append(json).append(']');
//		return json.toString();
//	}
//
//	public String map2Json(Map<String, Object> map) {
//		if (UtilValidate.isEmpty(map)) {
//			return "{}";
//		}
//
//		StringBuilder object = new StringBuilder();
//		for (String key : map.keySet()) {
//			if (object.length() != 0) {
//				object.append(',');
//			}
//			object.append(key).append(':');
//			Object value = map.get(key);
//			if (value instanceof List) {
//				object.append(list2Json((List) value));
//			} else if (value instanceof Map) {
//				object.append(map2Json((Map<String, Object>) value));
//			} else if (value instanceof String) {
//				object.append('\'').append(value).append('\'');
//			} else {
//				object.append(value);
//			}
//		}
//		object = new StringBuilder().append('{').append(object).append('}');
//
//		return object.toString();
//	}
//
//	public void setPermissionTypeService(PermissionTypeSer permissionTypeService) {
//		this.permissionTypeService = permissionTypeService;
//	}
//
//	public void setPermissionService(PermissionSer permissionService) {
//		this.permissionService = permissionService;
//	}
//
//	public void setRoleSecurityService(RoleSecurityService roleSecurityService) {
//		this.roleSecurityService = roleSecurityService;
//	}
//
//	public void setSecSRoleService(PartySrole secSRoleService) {
//		this.secSRoleService = secSRoleService;
//	}
//
//	public RoleSecurityService getRoleSecurityService() {
//		return roleSecurityService;
//	}

}
